package com.winz.cultureapp.activity;

import android.content.Intent;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.vp.NewsVpAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.entity.CulturalTypeBean;
import com.winz.cultureapp.fragment.MoreArtFragment;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文物鉴赏
 */
public class MoreArtActivity extends BaseAppCompatActivity {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.stb_art)
    SlidingTabLayout mStb_art;
    @BindView(R.id.vp_art)
    ViewPager mVp_art;

    private String[] titles;
    private List<Fragment> fragments = new ArrayList<>();
    private NewsVpAdapter mAdapter;

    @Override
    protected int contentView() {
        return R.layout.activity_more_act;
    }

    @Override
    protected void initViews() {
        mTv_top_title.setText(R.string.more_act_top_title);

    }

    @Override
    protected void initEvents() {

    }

    @Override
    protected void loadDatas(Intent intent) {
        getTypeList();
    }

    private void getTypeList() {
        showProgressDialog();
        HttpUitls.onGet("cultural_category",new OnResponeListener<CulturalTypeBean>() {
            @Override
            public void onSuccess(CulturalTypeBean bean) {
                Logger.d("cultural_category==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    // 可以初始化导航
                    initTitles(bean.data);
                    // 初始化界面
                    initFragments(bean.data);

                    mAdapter = new NewsVpAdapter(getSupportFragmentManager(), fragments, titles);
                    mVp_art.setAdapter(mAdapter);

                    mStb_art.setViewPager(mVp_art, titles);
                    mVp_art.setOffscreenPageLimit(titles.length);
                }else{
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("cultural_category==>>", error);
                dismissProgressDialog();
            }
        });

    }

    /**
     * 初始化导航标题
     * @param data
     */
    private void initTitles(CulturalTypeBean.DataBean data) {
        if (data.list.size()>0){
            titles = new String[data.list.size()];
            for (int i=0;i<data.list.size();i++){
                titles[i] = data.list.get(i).categoryName;
            }
        }
    }

    /**
     * 初始化界面
     */
    private void initFragments(CulturalTypeBean.DataBean data) {
        // 添加动态部分
        if (data.list.size()>0){
            for (int i=0;i<data.list.size();i++){
                MoreArtFragment f = MoreArtFragment.getInstance(data.list.get(i).museumGoodsCategoryId);
                fragments.add(f);
            }
        }
    }

    @OnClick({R.id.fl_back})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.fl_back:
                finish();
                break;
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
